/**
 * Copyright (c) 2008, Piercarlo Slavazza
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * The name of Piercarlo Slavazza cannot be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Piercarlo Slavazza ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Piercarlo Slavazza BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.libapml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

import com.libapml.util.Helper;

public class ApmlIndex implements Closable {

	private Hashtable<LuceneDocument, List<ConceptOccurrenceInDocument>> documents2ConceptOccurrencies = 
		new Hashtable<LuceneDocument, List<ConceptOccurrenceInDocument>>();

	private Hashtable<String, Concept> terms2Concepts = new Hashtable<String, Concept>();
	
	private List<Concept> sortedConcepts = null;
	
	private boolean closed = false;
	
	protected synchronized boolean addConcept(Concept concept) {
		
		boolean added = false;
		if (terms2Concepts.get(concept.getTerm()) == null) {
			terms2Concepts.put(concept.getTerm(), concept);
			added = true;
		}
		
		return added;
	}
	
	public synchronized Concept getConcept(String term) {
		return terms2Concepts.get(term);
	}
	
	public synchronized List<Concept> getAllConcepts() {
		return new ArrayList<Concept>(terms2Concepts.values());
	}
	
	public synchronized List<Concept> getSortedConcepts() {
		
		Helper.checkIsClosed(this);
		
		if (sortedConcepts == null) {
			
			sortedConcepts = getAllConcepts();
			Collections.sort(sortedConcepts, new ConceptTfIdfComparator());
			Collections.reverse(sortedConcepts);
			
		}
		
		return new ArrayList<Concept>(sortedConcepts);
	}
	
	protected synchronized void addConceptOccurrenceInDocument(
			LuceneDocument document, 
			ConceptOccurrenceInDocument occurrence) {
		
		List<ConceptOccurrenceInDocument> occurrencies = documents2ConceptOccurrencies.get(document);
		if (occurrencies == null)
			occurrencies = new ArrayList<ConceptOccurrenceInDocument>();
		
		occurrencies.add(occurrence);
		documents2ConceptOccurrencies.put(document, occurrencies);
		
	}
	
	public synchronized List<ConceptOccurrenceInDocument> getConceptOccurrencies(LuceneDocument document) {
		return documents2ConceptOccurrencies.get(document);
	}
	
	public synchronized int getDocumentCount() {
		
		return documents2ConceptOccurrencies.keySet().size();
		
	}
	
	public synchronized void close() {
		
		if (!closed) {
			
			int documentCount = getDocumentCount();
			
			for (Concept concept : getAllConcepts())
				concept.close(documentCount);

			closed = true;
		}
	}

	public synchronized boolean isClosed() {
		return closed;
	}
}
